$(function () {
    $("#jqGrid").jqGrid({
        url: '../labordispute/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true,hidden:true },
			{ label: '员工姓名', name: 'name', index: 'name', width: 80 }, 			
			{ label: '身份证号', name: 'idno', index: 'idno', width: 80 },
			{ label: '公司名称', name: 'company', index: 'company', width: 120 },
			{ label: '公司id', name: 'companyId', index: 'company_id', width: 80 ,hidden:true},
			{ label: '部门', name: 'dept', index: 'dept', width: 100 },
			{ label: '岗位名称', name: 'post', index: 'post', width: 80 }, 			
			{ label: '岗位id', name: 'postId', index: 'post_id', width: 80 ,hidden:true},
			{ label: '争议概况', name: 'disputeOverview', index: 'dispute_overview', width: 80 }, 			
			{ label: '争议类型', name: 'disputeType', index: 'dispute_type', width: 80 ,formatter:getDictInfo},
			{ label: '一级调整说明', name: 'firAdjustExplain', index: 'fir_adjust_explain', width: 120 },
			{ label: '一级调整结果', name: 'firAdjustResult', index: 'fir_adjust_result', width: 100 ,
                formatter:function(v){
                    if(v=="0"){
                        return "一致";
                    }else if(v=="1"){
                        return "不一致"
                    }else {
                        return ""
                    }
                } },
			{ label: '二级调整说明', name: 'secAdjustExplain', index: 'sec_adjust_explain', width: 120 },
			{ label: '二级调整结果', name: 'secAdjustResult', index: 'sec_adjust_result', width: 100 ,
                formatter:function(v){
                    if(v=="0"){
                        return "一致";
                    }else if(v=="1"){
                        return "不一致"
                    }else {
                        return ""
                    }
                } },
			{ label: '仲裁部门', name: 'arbitrationDepartment', index: 'arbitration_department', width: 120,formatter:getDictInfo },
			{ label: '仲裁结果', name: 'arbitrationResult', index: 'arbitration_result', width: 100 ,
                formatter:function(v){
                    if(v=="0"){
                        return "一致";
                    }else if(v=="1"){
                        return "不一致"
                    }else {
                        return ""
                    }
                } },
			{ label: '仲裁日期', name: 'arbitrationTime', index: 'arbitration_time',  width: 90, formatter:"date", formatoptions: {newformat:'Y-m-d'}  },
			{ label: '是否生效', name: 'iseffective', index: 'iseffective', width: 80 ,
                formatter:function(v){
                    if(v=="0"){
                        return "是";
                    }else if(v=="1"){
                        return "否"
                    }else {
                        return ""
                    }
                } },
			{ label: '制表人', name: 'cusername', index: 'cusername', width: 80 }, 			
			{ label: '录入日期', name: 'cdate', index: 'cdate',  width: 100, formatter:"date", formatoptions: {newformat:'Y-m-d'}  }
        ],
		viewrecords: true,
        height: 310,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        shrinkToFit:false,
        autoScroll: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });

 /*   $("#popGrid").jqGrid({
        url: '../safetyeducationmanagement/listForPop',
        datatype: "json",
        colModel: [
            { label: 'id', name: 'id', index: 'id', width: 50, key: true },
            { label: '员工姓名', name: 'name', index: 'name', width: 80 },
            { label: '身份证号', name: 'idno', index: 'idno', width: 80 },
            { label: '公司名称', name: 'company', index: 'company', width: 120 },
            { label: '公司id', name: 'companyId', index: 'company_id', width: 80 ,hidden:true},
            { label: '部门', name: 'department', index: 'department', width: 100 },
            { label: '岗位名称', name: 'post', index: 'post', width: 100 },
            { label: '入职日期', name: 'joinDate', index: 'join_date',  width: 90, formatter:"date", formatoptions: {newformat:'Y-m-d'}
            },
            { label: '性别', name: 'sex', index: 'sex', width: 80 },
            { label: '电话号码', name: 'telphone', index: 'telphone', width: 100,hidden:true }

        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: false,
        pager: "#popGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#popGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });*/
    vm.getDictList();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            dept: null,
            name: null
        },
        showList: true,
        showtable:false,
        showtable1:false,
        showtable2:false,
        showtable3:false,
        showtable4:false,
		title: null,
        dictList1:{},
        dictList2:{},
		laborDispute: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
            vm.showtable=false;
			vm.title = "新增";
			vm.laborDispute = {
			    name:""
            };
            vm.approveType="";

		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            if(rowData.firAdjustResult!=''&&rowData.firAdjustResult!=null)
            {
                alert("数据已经调解过，不能修改");
                return ;
            }
			vm.showList = false;
            vm.showtable=false;
            vm.title = "修改";
            // this.getDictList();
            vm.getInfo(id)
            vm.approveType="";

        },
		saveOrUpdate: function (event) {
            if(vm.approveType=='fir'){
                var firAdjustResult=vm.laborDispute.firAdjustResult;
                if(firAdjustResult==""||firAdjustResult==null){
                    alert("请填写调解结果");
                    return;
                }

            }else   if(vm.approveType=='sec'){
                var secAdjustResult=vm.laborDispute.secAdjustResult;
                if(secAdjustResult==""||secAdjustResult==null){
                    alert("请填写调解结果");
                    return;
                }

            }else   if(vm.approveType=='thi'){
                var arbitrationResult=vm.laborDispute.arbitrationResult;
                if(arbitrationResult==""||arbitrationResult==null){
                    alert("请填写仲裁结果");
                    return;
                }

            }else   if(vm.approveType=='fou'){
                var iseffective=vm.laborDispute.iseffective;
                if(iseffective==""||iseffective==null){
                    alert("请填写是否生效");
                    return;
                }

            }
            vm.laborDispute.arbitrationTime=$("#arbitrationTime").val();
           /* if(vm.approveType=='fir'||vm.approveType=='sec'||vm.approveType=='thi')
            { vm.laborDispute.iseffective=vm.approveType;//暂时存放
            }*/
            var url = vm.laborDispute.id == null ? "../labordispute/save" : "../labordispute/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.laborDispute),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var len=ids.length;
			for(var i=0;i<len;i++){
                var rowData=$("#jqGrid").jqGrid("getRowData",ids[i]);
                if(rowData.firAdjustResult!=''&&rowData.firAdjustResult!=null)
                {
                    alert("数据已经调解过，不能删除");
                    return ;
                }
            }
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../labordispute/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../labordispute/info/"+id, function(r){
                vm.laborDispute = r.laborDispute;
            });
		},
		reload: function (event) {
            vm.showList = true;
            vm.showtable = false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'name': vm.q.name, 'company': vm.q.company, 'post': vm.q.post,
                    'dept': vm.q.dept},
                page:page
            }).trigger("reloadGrid");
		},
        firApprove: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            if(rowData.iseffective!=''&&rowData.iseffective!=null)
            {
                alert("数据已生效，不能调解");
                return ;
            }

            var firAdjustResult=rowData.firAdjustResult;
            if(firAdjustResult!=""&&firAdjustResult!=null){
                alert("已进行一级调解，不能重复调解");
                return;
            }
            vm.showList = false;
            vm.showtable=true;
            vm.showtable1=true;
            vm.showtable2=false;
            vm.showtable3=false;
            vm.showtable4=false;
            vm.title = "一级调解结果";
            vm.getInfo(id);
            vm.approveType="fir";
            vm.laborDispute.firAdjustResult='0';
        },
        secApprove: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            if(rowData.iseffective!=''&&rowData.iseffective!=null)
            {
                alert("数据已生效，不能调解");
                return ;
            }

            var firAdjustResult=rowData.firAdjustResult;
            var secAdjustResult=rowData.secAdjustResult;
            if(firAdjustResult=="一致"){
                alert("一级调解达到目标，调解结束");
                return;
            }
            if(firAdjustResult==""||firAdjustResult==null){
                alert("请先进行一级调解");
                return;
            }
            if(secAdjustResult!=""&&secAdjustResult!=null){
                alert("二级调解已进行过，不能重复调解");
                return;
            }

            vm.showList = false;
            vm.showtable=true;
            vm.showtable1=false;
            vm.showtable2=true;
            vm.showtable3=false;
            vm.showtable4=false;
            vm.title = "二级调解结果";
            vm.getInfo(id);
            vm.approveType="sec";
            vm.laborDispute.secAdjustResult='0';


        },
        thiApprove: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);
            if(rowData.iseffective!=''&&rowData.iseffective!=null)
            {
                alert("数据已生效，不能仲裁结果");
                return ;
            }


            var arbitrationResult=rowData.arbitrationResult;
            var secAdjustResult=rowData.secAdjustResult;
            if(secAdjustResult=="一致"){
                alert("二级调解达到目标，调解结束");
                return;
            }
            if(secAdjustResult==""||secAdjustResult==null){
                alert("请先进行二级调解");
                return;
            }
            if(arbitrationResult!=""&&arbitrationResult!=null){
                alert("已执行过仲裁结果，不能重复仲裁");
                return;
            }

            vm.showList = false;
            vm.showtable=true;
            vm.showtable1=false;
            vm.showtable2=false;
            vm.showtable3=true;
            vm.showtable4=false;
            vm.title = "仲裁结果";
            vm.getInfo(id);
            vm.approveType="thi";
            vm.laborDispute.arbitrationResult='0';



        },
        fouApprove: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            var rowData=$("#jqGrid").jqGrid("getRowData",id);

            if(rowData.iseffective!=''&&rowData.iseffective!=null)
            {
                alert("数据已生效，不能执行结果");
                return ;
            }

            var arbitrationResult=rowData.arbitrationResult;
            //如果一二调解为一致，都可以执行结果，如果都是不一致，则需要仲裁才能执行结果
            if(rowData.firAdjustResult=="一致"){

            }else
            if(rowData.secAdjustResult=="一致"){

            }else
            if(arbitrationResult==""||arbitrationResult==null){
                alert("请先进行仲裁");
                return;
            }


            vm.showList = false;
            vm.showtable=true;
            vm.showtable1=false;
            vm.showtable2=false;
            vm.showtable3=false;
            vm.showtable4=true;
            vm.title = "执行结果";
            vm.getInfo(id);
             vm.approveType="fou";
            vm.laborDispute.iseffective='0';


        },

        popPage: function(){
            utils.popGrid();
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择员工",
                area: ['800px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#popLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var grid = $("#popGrid");
                    var rowKey = grid.getGridParam("selrow");
                    if(!rowKey){
                        alert("请选择一条记录");
                        return ;
                    }
                    var rowData=grid.jqGrid("getRowData",rowKey);
                    vm.laborDispute.name=rowData.name;
                    vm.laborDispute.idno=rowData.idno;
                    vm.laborDispute.company=rowData.company;
                    vm.laborDispute.companyId=rowData.companyId;
                    vm.laborDispute.dept=rowData.department;
                    vm.laborDispute.post=rowData.post;
                    //alert(JSON.stringify( vm.safetyEducationManagement));


                    layer.close(index);
                }
            });
        },
        getDictList: function(){
            var params = {
                dicttype:"zylx,zcbm"
            }
            $.ajax({
                type: "POST",
                url: "../dictutils/getDictList",
                contentType: "application/json",
                data: JSON.stringify(params),
                dataType:"json",
                async:false,
                cache:false,
                success: function(r){
                    if(r.code === 0){
                        vm.dictList1 = r.zylx;
                        vm.dictList2 = r.zcbm;
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
	}
});